

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head><!-- hexo injector head_begin start --><link rel="stylesheet" href="/css/my-avatar.css"><!-- hexo injector head_begin end -->
  <meta charset="UTF-8">

  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/icon/coffee.png">
  

  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="晓寒">
  <meta name="keywords" content="">
  
    <meta name="description" content="哈希算法解析与实践">
<meta property="og:type" content="article">
<meta property="og:title" content="哈希算法">
<meta property="og:url" content="http://example.com/posts/60365/index.html">
<meta property="og:site_name" content="Xiaohan&#96;s Blog">
<meta property="og:description" content="哈希算法解析与实践">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-11-30T04:40:53.000Z">
<meta property="article:modified_time" content="2025-03-17T15:16:22.906Z">
<meta property="article:author" content="晓寒">
<meta property="article:tag" content="算法学习">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>哈希算法 - Xiaohan`s Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1749284_5i9bdhy70f8.css">



<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1736178_k526ubmyhba.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/css/mac.css">
<link rel="stylesheet" href="/css/shubiao.css">
<link rel="stylesheet" href="/css/huadong.css">
<link rel="stylesheet" href="/css/fonts.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.8","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":true,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false},"umami":{"src":null,"website_id":null,"domains":null,"start_time":"2024-01-01T00:00:00.000Z","token":null,"api_server":null}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head>


<body><!-- hexo injector body_begin start --><div id="web_bg"></div><!-- hexo injector body_begin end -->
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Xiaohan`s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>Home</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>Category</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>Tag</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>Archive</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/" target="_self">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>About</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/bg/mountains.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="哈希算法"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-11-30 12:40" pubdate>
          2022年11月30日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          2.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          18 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">哈希算法</h1>
            
              <p id="updated-time" class="note note-info" style="">
                
                  
                    本文最后更新于 2025年3月17日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>s# 前言</p>
<p>对于字符串的比较，如果我们想要判断这两个字符串是否相同，我们需要对字符串的每一个字符进行比较，但这实在是太费时了。那么有没有一种方法能让字符串的比较和数字的比较一样容易呢？哈希算法我为我们提供了思路。</p>
<hr />
<h1 id="单模哈希">单模哈希</h1>
<h2 id="原理">原理</h2>
<p>将字符串的ASCII码分别乘上a求和后再对b取模，最后得出Hash值，我们比较这两个字符串只需要比较它们的Hash值即可。
说明：a,b是任意设置的数，不过我们一般让b尽量大，因为b越大Hash值的情况越多，出现两个不同字符串的Hash值越小，而a是为了防止不同位数的字符串Hash值相同。</p>
<h2 id="缺点">缺点</h2>
<p>看完原理我们就会发现哈希算法其实并不算完全严谨的算法，它是通过使不同字符串Hash值相同的概率趋近于0来保证字符串比较的正确性。</p>
<h2 id="例题1hdoj-1004">例题1：HDOJ 1004</h2>
<h3 id="题目">题目</h3>
<h4 id="problem-description">Problem Description</h4>
<p>Contest time again! How excited it is to see balloons floating
around. But to tell you a secret, the judges' favorite time is guessing
the most popular problem. When the contest is over, they will count the
balloons of each color and find the result.</p>
<p>This year, they decide to leave this lovely job to you.</p>
<h4 id="input">Input</h4>
<p>Input contains multiple test cases. Each test case starts with a
number N (0 &lt; N &lt;= 1000) -- the total number of balloons
distributed. The next N lines contain one color each. The color of a
balloon is a string of up to 15 lower-case letters.</p>
<p>A test case with N = 0 terminates the input and this test case is not
to be processed.</p>
<h4 id="output">Output</h4>
<p>For each case, print the color of balloon for the most popular
problem on a single line. It is guaranteed that there is a unique
solution for each test case.</p>
<h4 id="sample-input">Sample Input</h4>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs input">5<br>green<br>red<br>blue<br>red<br>red<br>3<br>pink<br>orange<br>pink<br>0<br></code></pre></td></tr></table></figure>
<h4 id="sample-output">Sample Output</h4>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs output">red<br>pink<br></code></pre></td></tr></table></figure>
<h3 id="题解">题解</h3>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;string.h&gt;</span>  </span><br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span>  <br>&#123;  <br>    <span class="hljs-type">long</span> <span class="hljs-type">long</span> <span class="hljs-type">int</span> m;  <br>    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) &#123;  <br>        <span class="hljs-type">int</span> n;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;n);  <br>        <span class="hljs-keyword">if</span>(n==<span class="hljs-number">0</span>)<span class="hljs-keyword">break</span>;  <br>        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">colors</span> &#123;</span>  <br>            <span class="hljs-type">long</span> <span class="hljs-type">long</span> <span class="hljs-type">int</span> sum;  <br>            <span class="hljs-type">int</span> count;  <br>            <span class="hljs-type">char</span> arr1[<span class="hljs-number">16</span>];  <br>        &#125;;  <br>        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">colors</span> <span class="hljs-title">color</span>[1003];</span><br>        <span class="hljs-comment">//初始化结构体  </span><br>        <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">1</span>;i&lt;=<span class="hljs-number">1001</span>;i++)&#123;  <br>            color[i].sum=<span class="hljs-number">0</span>;  <br>            color[i].count=<span class="hljs-number">0</span>;  <br>        &#125;  <br>        <span class="hljs-type">int</span> st=<span class="hljs-number">0</span>,max1=<span class="hljs-number">1</span>;  <br>        <span class="hljs-type">char</span> arr[<span class="hljs-number">16</span>];  <br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) &#123;  <br>            m = <span class="hljs-number">0</span>;  <br>            <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%s&quot;</span>, arr);  <br>            <span class="hljs-type">char</span> *t = arr;  <br>            <span class="hljs-comment">//将不同的颜色转化为长整型进行计数（哈希）</span><br>            <span class="hljs-keyword">while</span> (*t != <span class="hljs-string">&#x27;\0&#x27;</span>) &#123;  <br>                m += *t + <span class="hljs-number">133331</span>;  <br>                t++;  <br>            &#125;  <br>            <span class="hljs-type">int</span> j;  <br>            <span class="hljs-keyword">for</span> (j = <span class="hljs-number">1</span>; j &lt;= st; j++) &#123;  <br>                <span class="hljs-keyword">if</span> (color[j].sum == m) &#123;  <br>                    color[j].count++;  <br>                &#125;  <br>            &#125;  <br>            <span class="hljs-keyword">if</span> (j == st + <span class="hljs-number">1</span>) &#123;  <br>                st++;  <br>                <span class="hljs-built_in">strcpy</span>(color[st].arr1, arr);  <br>                color[st].count++;  <br>                color[st].sum = m;  <br>            &#125;  <br>            <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> j = <span class="hljs-number">1</span>; j &lt;= st; j++) &#123;  <br>                <span class="hljs-keyword">if</span> (color[j].count &gt; color[max1].count)max1 = j;  <br>            &#125;  <br>        &#125;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s\n&quot;</span>, color[max1].arr1);  <br>    &#125;  <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>&#125;<br></code></pre></td></tr></table></figure>
<hr />
<h2
id="例题2p3370-模板字符串哈希---洛谷-计算机科学教育新生态-luogu.com.cn">例题2：<a
target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P3370">P3370 【模板】字符串哈希 -
洛谷 | 计算机科学教育新生态 (luogu.com.cn)</a></h2>
<h3 id="题目描述">题目描述</h3>
<p>如题，给定 <span class="math inline">\(N\)</span> 个字符串（第 <span
class="math inline">\(i\)</span> 个字符串长度为 <span
class="math inline">\(M_i\)</span>，字符串内包含数字、大小写字母，大小写敏感），请求出
<span class="math inline">\(N\)</span>
个字符串中共有多少个不同的字符串。</p>
<p><strong>友情提醒：如果真的想好好练习哈希的话，请自觉。</strong></p>
<h3 id="输入格式">输入格式</h3>
<p>第一行包含一个整数 <span
class="math inline">\(N\)</span>，为字符串的个数。</p>
<p>接下来 <span class="math inline">\(N\)</span>
行每行包含一个字符串，为所提供的字符串。</p>
<h3 id="输出格式">输出格式</h3>
<p>输出包含一行，包含一个整数，为不同的字符串个数。</p>
<h3 id="样例-1">样例 #1</h3>
<h4 id="样例输入-1">样例输入 #1</h4>
<figure class="highlight vim"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs vim"><span class="hljs-number">5</span><br><span class="hljs-keyword">abc</span><br>aaaa<br><span class="hljs-keyword">abc</span><br>abcc<br><span class="hljs-number">12345</span><br></code></pre></td></tr></table></figure>
<h4 id="样例输出-1">样例输出 #1</h4>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs">4<br></code></pre></td></tr></table></figure>
<h3 id="提示">提示</h3>
<p>对于 <span class="math inline">\(30\%\)</span> 的数据：<span
class="math inline">\(N\leq 10\)</span>，<span
class="math inline">\(M_i≈6\)</span>，<span
class="math inline">\(Mmax\leq 15\)</span>。</p>
<p>对于 <span class="math inline">\(70\%\)</span> 的数据：<span
class="math inline">\(N\leq 1000\)</span>，<span
class="math inline">\(M_i≈100\)</span>，<span
class="math inline">\(Mmax\leq 150\)</span>。</p>
<p>对于 <span class="math inline">\(100\%\)</span> 的数据：<span
class="math inline">\(N\leq 10000\)</span>，<span
class="math inline">\(M_i≈1000\)</span>，<span
class="math inline">\(Mmax\leq 1500\)</span>。</p>
<p>样例说明：</p>
<p>样例中第一个字符串(abc)和第三个字符串(abc)是一样的，所以所提供字符串的集合为{aaaa,abc,abcc,12345}，故共计4个不同的字符串。</p>
<h3 id="题解-1">题解</h3>
<p>根据哈希算法的原理构建代码。不过要注意的是，因为无符号长整型在数值溢出时会自动对<span
class="math inline">\(2^{64}\)</span>取模，因此就不手动取模了。</p>
<p>代码如下： <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;string.h&gt;</span>  </span><br><span class="hljs-type">long</span> <span class="hljs-type">long</span> <span class="hljs-type">unsigned</span> <span class="hljs-type">int</span> arr[<span class="hljs-number">10003</span>];  <br><span class="hljs-type">char</span> ch[<span class="hljs-number">10003</span>];  <br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span> &#123;  <br>    <span class="hljs-type">int</span> n;  <br>    <span class="hljs-type">int</span> count=<span class="hljs-number">0</span>;  <br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;n);  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">1</span>;i&lt;=n;i++)&#123;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%s&quot;</span>,ch);  <br>        <span class="hljs-type">int</span> len=<span class="hljs-built_in">strlen</span>(ch);  <br>        <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> j=<span class="hljs-number">0</span>;j&lt;len;j++)&#123;  <br>            arr[i]=arr[i]*<span class="hljs-number">13331</span>+ch[j];  <br>        &#125;  <br>        count++;  <br>        <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> k=<span class="hljs-number">1</span>;k&lt;i;k++)&#123;  <br>            <span class="hljs-keyword">if</span>(arr[k]==arr[i])&#123;  <br>                count--;  <br>                <span class="hljs-keyword">break</span>;  <br>            &#125;  <br>        &#125;  <br>    &#125;  <br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d&quot;</span>,count);  <br>&#125;<br></code></pre></td></tr></table></figure></p>
<hr />
<h1 id="双模哈希">双模哈希</h1>
<p>即使我们减少了两个不同字符串Hash值大小相同的概率，但是不排除偶然出现的情况，为了进一步使哈希算法能够达到我们的预期，我们可以分别对两个数取模，并认定同时满足的时候两个字符串是相等的，这样及基本上不会出现问题。</p>
<p>因为没有找到适合的题目，这里就用C++给出示范代码。（看懂就行，代码没有实际意义）</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;bits/stdc++.h&gt;</span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><span class="hljs-keyword">using</span> ll=<span class="hljs-type">long</span> <span class="hljs-type">long</span>;<br><span class="hljs-keyword">using</span> ull=<span class="hljs-type">unsigned</span> <span class="hljs-type">long</span> <span class="hljs-type">long</span>;<br><span class="hljs-type">const</span> <span class="hljs-type">int</span> N=<span class="hljs-number">1e6</span>+<span class="hljs-number">6</span>,mod=<span class="hljs-number">1e9</span>+<span class="hljs-number">7</span>;<br>ull kpow1[N],hs1[N];<span class="hljs-comment">//% 2^64</span><br>ll kpow2[N],hs2[N];<span class="hljs-comment">//% mod</span><br><span class="hljs-type">char</span> s[N];<span class="hljs-type">int</span> n;<br>pair&lt;ull,ll&gt;<span class="hljs-built_in">gethash</span>(<span class="hljs-type">int</span> l,<span class="hljs-type">int</span> r)&#123;<br>	ull a=hs1[r]-hs1[l<span class="hljs-number">-1</span>]*kpow1[r-l+<span class="hljs-number">1</span>];<br>	ll b=hs2[r]-hs2[l<span class="hljs-number">-1</span>]*kpow2[r-l+<span class="hljs-number">1</span>];<br>	b=(b%mod+mod)%mod;<br>	<span class="hljs-keyword">return</span> &#123;a,b&#125;;<br>&#125;<span class="hljs-comment">//pair是C++中的配对，可以类比C语言中的结构体</span><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>&#123;<br>	kpow1[<span class="hljs-number">0</span>]=kpow2[<span class="hljs-number">0</span>]=<span class="hljs-number">1</span>;<br>	<span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">1</span>;i&lt;N;++i)&#123;<br>		kpow1[i]=kpow1[i<span class="hljs-number">-1</span>]*<span class="hljs-number">13331</span>;<br>		kpow2[i]=kpow2[i<span class="hljs-number">-1</span>]*<span class="hljs-number">13331</span>%mod;<br>	&#125;<br>	<span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%s&quot;</span>,s+<span class="hljs-number">1</span>);n=<span class="hljs-built_in">strlen</span>(s+<span class="hljs-number">1</span>);<br>	<span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">1</span>;i&lt;=n;++i)&#123;<br>		hs1[i]=hs1[i<span class="hljs-number">-1</span>]*<span class="hljs-number">13331</span>+s[i];<br>		hs2[i]=(hs2[i<span class="hljs-number">-1</span>]*<span class="hljs-number">13331</span>+s[i])%mod;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<hr />
<h1 id="开阔视野哈希算法在密码学中">开阔视野（哈希算法在密码学中）</h1>
<p><strong>Hash的特点</strong></p>
<p>易压缩：对于任意大小的输入x，Hash值的长度很小，在实际应用中，函数H产生的Hash值其长度是固定的。</p>
<p>易计算：对于任意给定的消息，计算其Hash值比较容易。</p>
<p>单向性：对于给定的Hash值，要找到使得在计算上是不可行的，即求Hash的逆很困难。在给定某个哈希函数H和哈希值H（M）的情况下，得出M在计算上是不可行的。即从哈希输出无法倒推输入的原始数值。这是哈希函数安全性的基础。</p>
<p>抗碰撞性：理想的Hash函数是无碰撞的，但在实际算法的设计中很难做到这一点。</p>
<p>有两种抗碰撞性：一种是弱抗碰撞性，即对于给定的消息，要发现另一个消息，满足在计算上是不可行的；另一种是强抗碰撞性，即对于任意一对不同的消息，使得在计算上也是不可行的。</p>
<p>高灵敏性：这是从比特位角度出发的，指的是1比特位的输入变化会造成1/2的比特位发生变化。消息M的任何改变都会导致哈希值H（M）发生改变。即如果输入有微小不同，哈希运算后的输出一定不同。</p>
<p><strong>哈希算法有什么用途？</strong></p>
<p>哈希算法可以检验信息是否是相同的，这样的优势可以节省重复文件传送的时间。</p>
<p>举一个生活中很平常的例子，我们在生活工作中会使用一些软件给别人传送文件数据，如果有人传送了一份文件给一个人，然后又有一个人传送了相同的文件给了另外一个人，那么这个社交软件在第二次传送文件的时候会对比两次传送的哈希值，发现是相同的，该软件就不会再次上传文件给服务器了。</p>
<p>除此之外，哈希算法还可以检验信息的拥有者是否真实。</p>
<p>比如，我们在一个网站注册一个账号，如果网站把密码保存起来，那这个网站不论有多安全，也会有被盗取的风险。但是如果用保存密码的哈希值代替保存密码，就没有这个风险了，因为哈希值加密过程是不不可逆的。</p>
<p><strong>哈希算法会不会被破解？</strong></p>
<p>从理论上说，哈希值是可以被获得的，但是对应的用户密码很难获得。</p>
<p>假设一个网站被攻破，黑客获得了哈希值，但仅仅只有哈希值还不能登录网站，他还必须算出相应的账号密码。</p>
<p>计算密码的工作量是非常庞大且繁琐的，严格来讲，密码是有可能被破译的，但破译成本太大，被成功破译的几率很小，所以基本是不用担心密码泄露的。</p>
<p>当然，黑客们还可以采用一种物理方法，那就是猜密码。他可以随机一个一个的试密码，如果猜的密码算出的哈希值正好与真正的密码哈希值相同，那么就说明这个密码猜对了。</p>
<p>密码的长度越长，密码越复杂，就越难以猜正确。如果有一种方法能够提高猜中密码的可能，那么可以算是哈希算法被破解了。</p>
<p>开阔视野内容转载自<a
target="_blank" rel="noopener" href="https://www.cnblogs.com/zfss/p/11065234.html">紫天峰的博客</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E7%AE%97%E6%B3%95%E4%B8%93%E6%A0%8F/" class="category-chain-item">算法专栏</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E7%AE%97%E6%B3%95%E5%AD%A6%E4%B9%A0/" class="print-no-link">#算法学习</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>哈希算法</div>
      <div>http://example.com/posts/60365/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>晓寒</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年11月30日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-cc-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/54862/" title="链表">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">链表</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/posts/10531/" title="KMP字符串匹配">
                        <span class="hidden-mobile">KMP字符串匹配</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments">
      
  <script type="text/javascript">
    Fluid.utils.loadComments('#comments', function() {
      var light = 'github-light';
      var dark = 'github-dark';
      var schema = document.documentElement.getAttribute('data-user-color-scheme');
      if (schema === 'dark') {
        schema = dark;
      } else {
        schema = light;
      }
      window.UtterancesThemeLight = light;
      window.UtterancesThemeDark = dark;
      var s = document.createElement('script');
      s.setAttribute('src', 'https://utteranc.es/client.js');
      s.setAttribute('repo', 'Dion6850/Dion6850.github.io');
      s.setAttribute('issue-term', 'title');
      
      s.setAttribute('theme', schema);
      s.setAttribute('crossorigin', 'anonymous');
      document.getElementById('comments').appendChild(s);
    })
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  


  
  









    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> <div style="font-size: 0.85rem"> <span id="timeDate">载入天数...</span> <span id="times">载入时分秒...</span> <script src="/css/time.js"></script> </div> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    

  

</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


    <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
    <script>
        (function (window, document) {
            var typing = Fluid.plugins.typing;
            var subtitle = document.getElementById('subtitle');
            if (!subtitle || !typing) {
                return;
            }
            var text = subtitle.getAttribute('data-typed-text');
            
            typing(text);
            
        })(window, document);
    </script>
 


  
    
      <script  src="/js/img-lazyload.js" ></script>
    
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/5.0.0/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  
      <script>
        if (!window.MathJax) {
          window.MathJax = {
            tex    : {
              inlineMath: { '[+]': [['$', '$']] }
            },
            loader : {
              load: ['ui/lazy']
            },
            options: {
              renderActions: {
                insertedScript: [200, () => {
                  document.querySelectorAll('mjx-container').forEach(node => {
                    let target = node.parentNode;
                    if (target.nodeName.toLowerCase() === 'li') {
                      target.parentNode.classList.add('has-jax');
                    }
                  });
                }, '', false]
              }
            }
          };
        } else {
          MathJax.startup.document.state(0);
          MathJax.texReset();
          MathJax.typeset();
          MathJax.typesetPromise();
        }

        Fluid.events.registerRefreshCallback(function() {
          if ('MathJax' in window && MathJax.startup.document && typeof MathJax.startup.document.state === 'function') {
            MathJax.startup.document.state(0);
            MathJax.texReset();
            MathJax.typeset();
            MathJax.typesetPromise();
          }
        });
      </script>
    

  <script  src="https://lib.baomitu.com/mathjax/3.2.2/es5/tex-mml-chtml.js" ></script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="/css/time.js"></script>
<script src="/css/huadong.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<!-- hexo injector body_end start --><script src="/js/backgroundize.js"></script><!-- hexo injector body_end end --></body>
</html>
